package ar.com.bdf.ipharma.communications;

public class BufferBox {
	
	private StringBuffer buffer = new StringBuffer();
	private static final String LINE_SEPARATOR = "\n";
	private RunnableLine runner;

	/**
	 * Se crea el buffer con un Runnable que corra cada vez que se lee una linea nueva
	 * @param runner
	 */
	public BufferBox(RunnableLine runner) {
		super();
		this.runner = runner;
	}

	public synchronized void addInBuffer(String str) {
		buffer.append(str);
		
		int indexLineSeparator = buffer.indexOf(LINE_SEPARATOR);
		//runner.execute(str);
		
		if (indexLineSeparator != -1) {
			String line = buffer.substring(0, indexLineSeparator);
			runner.execute(line);
			buffer.delete(0, indexLineSeparator + LINE_SEPARATOR.length());
		}
	}

}
